﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Deanery.Controller.Directories
{
    class TypeEducationController
    {
        private readonly deaneryEntities _db = new deaneryEntities();

        /// <summary>
        /// This method transfers columns name in ukrainian for view table Type_Education
        /// </summary>
        /// <returns>Array of string with columns names</returns>
        public Array ColumnName()
        {
            string[] columnName = {"ID", "Форма навчання","Код ЄДБО"};
            return columnName;
        }

        /// <summary>
        /// This method transfers columns data type in table Type_Education
        /// </summary>
        /// <returns>Array of string with columns data type</returns>
        public Array ColumnValueType()
        {
            string[] columnDataType = {"guid","string", "int"};
            return columnDataType;
        }

        /// <summary>
        /// This method select all reccords from table Type_Education
        /// </summary>
        /// <returns>Set of IQueryable</returns>
        public IQueryable GetAll()
        {
           var list = _db.Type_Education.Select(f => f);
           return list;
        }

        /// <summary>
        /// This method performs INSERT one record to DB in table Type_Education
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="reccord">ArrayList(one item - one cell value) with data to insert</param>
        public void Insert(ArrayList reccord)
        {
            var obj = new Type_Education
                {
                    id_edu_type = GenerateGuid(),
                    nv_edu_type_name = reccord[0].ToString(),
                    int_edbo = Convert.ToInt32(reccord[1])
                };
            _db.Type_Education.AddObject(obj);
            _db.SaveChanges();
        }

        /// <summary>
        /// This method performs UPDATE one record to DB in table Type_Education
        /// PROBLEM: method returns nothing
        /// </summary>
        /// <param name="updateGuid">guid value to search reccord in table</param>
        /// <param name="reccord">ArrayList(one item - one cell value) with data to update</param>
        public void Update(Guid updateGuid, ArrayList reccord)
        {
            var obj = (_db.Type_Education.Where(t => t.id_edu_type == updateGuid)).Single();
            obj.nv_edu_type_name = reccord[0].ToString();
            obj.int_edbo = Convert.ToInt32(reccord[1]);
            _db.SaveChanges();
        }

        /// <summary>
        /// This method generates new guid
        /// </summary>
        /// <returns>New guid</returns>
        public Guid GenerateGuid()
        {
            var guid = Guid.NewGuid();
            return guid;
        }

        /// <summary>
        /// This method select guid from table Type_Education
        /// </summary>
        /// <param name="name">String value for search guid in table</param>
        /// <returns>Guid of passed parameter</returns>
        public Guid FindGuidByName(string name)
        {
            var guid = _db.Type_Education.Where(t => t.nv_edu_type_name == name).Select(t => t.id_edu_type).Single();
            return guid;
        }

        /// <summary>
        /// This method select all type_Education name from table Type_Education
        /// </summary>
        /// <returns>Array(every item is Type_Education name)</returns>
        public Array GetAllTypeEducationName()
        {
            var list = from t in _db.Type_Education select t.nv_edu_type_name;
            return list.ToArray();
        }
    }
}
